package ld.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ld.mapper.DeptMapper;
import ld.mapper.EmpMapper;
import ld.pojo.Emp;
import ld.pojo.EmpExample;
import ld.pojo.PageResult;
import ld.service.EmpService;
import ld.util.MybatisUtil;
import ld.vo.EmpVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Liudao
 * @Date: 2022-08-01-22:16
 * @Description:
 */
public class EmpServiceImpl implements EmpService {
    private EmpMapper empMapper;
    private DeptMapper deptMapper;

    @Override
    public PageResult<Emp> search(int page, int pagesize, EmpVo vo) {
        PageHelper.startPage(page, pagesize);
        //2.2 开始查询
        try (SqlSession session = MybatisUtil.getSqlSession()) {
            //2.2.1 得到Mapper对象
            empMapper = session.getMapper(EmpMapper.class);
            deptMapper = session.getMapper(DeptMapper.class);
            //2.2.2 定义条件查询实例对象
            EmpExample example = new EmpExample();
            //2.2.3 得到关联的条件对象
            EmpExample.Criteria criteria = example.createCriteria();
            //2.2.4 封装查询条件
            if (StringUtils.isNotBlank(vo.getEname())) {
                criteria.andEnameLike("%" + vo.getEname() + "%");
            }
            if (StringUtils.isNotBlank(vo.getCity())) {
                criteria.andCityLike("%" + vo.getCity() + "%");
            }
            if (vo.getDid() != 0) {
                criteria.andDidEqualTo(vo.getDid());
            }
            //2.2.5 开始查询
            List<Emp> emps = empMapper.selectByExample(example);
            //2.2.6 绑定部门名称
            for (Emp emp : emps) {
                emp.setDname(empMapper.selectByPrimaryKey(emp.getDid()).getDname());
            }
            //2.2.7 封装成PageInfo
            PageInfo<Emp> pageInfo = new PageInfo<>(emps);
            //2.2.8 返回自定义分页对象
            return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
        }


    }

    @Override
    public boolean add(Emp emp) {
        try(SqlSession session = MybatisUtil.getSqlSession()) {
            EmpMapper mapper = session.getMapper(EmpMapper.class);
            return mapper.insert(emp) != 0;
        }
    }

    @Override
    public boolean update(Emp emp) {
        try(SqlSession session = MybatisUtil.getSqlSession()) {
            EmpMapper mapper = session.getMapper(EmpMapper.class);
            return mapper.updateByPrimaryKey(emp) != 0;
        }
    }

    @Override
    public boolean delete(String[] eids) {
        try(SqlSession session = MybatisUtil.getSqlSession()) {
            //5.1 得到mapper
            EmpMapper mapper = session.getMapper(EmpMapper.class);
            //5.2 构造查询实例
            EmpExample example = new EmpExample();
            EmpExample.Criteria criteria = example.createCriteria();
             List<Integer> collect = Arrays.stream(eids).map(Integer::new).collect(Collectors.toList());
            //5.3 使用in子查询删除多个学生
//            List<Integer> list = new ArrayList<>();
//            for (String sid : sids) {
//                list.add(new Integer(sid));
//            }
            criteria.andEidIn(collect);
            //5.4 根据实例删除
            return mapper.deleteByExample(example) != 0;
        }
    }

    @Override
    public List<Emp> findAll() {
        try(SqlSession session = MybatisUtil.getSqlSession()) {
            //1.1 得到Mapper对象
            empMapper = session.getMapper(EmpMapper.class);
            deptMapper = session.getMapper(DeptMapper.class);
            //1.2 进行实例查询
            List<Emp> emps = empMapper.selectByExample(null);
            //1.3 绑定班级名称
            for (Emp emp : emps) {
                System.out.println(emp.getDid());
                emp.setDname(deptMapper.selectByPrimaryKey(emp.getDid()).getDname());
            }
            //1.4 返回
            return emps;
        }
    }
}
